//
//  ST_UnsatisfactoryReasonViewController.swift
//  btvideo
//
//  Created by mac on 2020/12/8.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes

class ST_UnsatisfactoryReasonViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(15)
            make.left.right.equalToSuperview()
            make.height.equalTo(338+BTSafeAreaBottom)
        }
        
        
        view.addSubview(close_view)
        close_view.snp.makeConstraints { (make) in
            make.top.right.left.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top)
        }
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
        }
        
        
        
        root_view.addSubview(confirm_btn)
        confirm_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(44)
            make.width.equalTo(260)
            make.bottom.equalToSuperview().offset(-20-BTSafeAreaBottom)
        }
        
        
        root_view.addSubview(pickerView)
        
        pickerView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom)
            make.bottom.equalTo(confirm_btn.snp.top)
        }
    }
    
    
    
//    MAKR:- Properties
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 15
        return control
    }()
    
    fileprivate lazy var close_view:UIControl = {
        let control = UIControl()
        control.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "不满意的原因是什么呢"
        control.textColor = UIColor(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    var type = 1
    
    var targetAccid = ""
    
    
    fileprivate lazy var optionStringArray = ["其他原因","通话质量原因","对方态度原因","对方声音不满意","对方恶意挂断"]
 
    fileprivate var pickedIndex = 0
    
    fileprivate lazy var pickerView:UIPickerView = {
        let control = UIPickerView()
        control.delegate = self
        control.dataSource = self
        return control
    }()
    
    
    
    fileprivate lazy var confirm_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("提交")
        control.setTitleColorForAllStates(UIColor.white)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.backgroundColor = UIColor.init(hexString: "#FEC207")
        control.cornerRadius = 22
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    @objc fileprivate func close_handle(){
        
        self.dismiss(animated: true, completion: nil)
        
    }
    
    var confirm_closure:((String)->())?
    
    @objc fileprivate func confirm_handle() {
        
        guard let str = optionStringArray[safe:self.pickedIndex] else{
            return
        }
        
        
        self.confirm_btn.isUserInteractionEnabled = false
                
        main_thread_execute(afterDelay: 2) {
            self.confirm_btn.isUserInteractionEnabled = true
        }
        
        
        
        self.dismiss(animated: true) {
            self.confirm_closure?(str)
        }
        
        
    }
}



extension ST_UnsatisfactoryReasonViewController:UIPickerViewDelegate,UIPickerViewDataSource{
    
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        return optionStringArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        
        let str = optionStringArray[safe:row]?.withAttributes([Attribute.textColor(UIColor.init(hex:0x333333)!),Attribute.font(Font.systemFont(ofSize: 16, weight: UIFont.Weight.medium))])
        
        return str
    }
    
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        return optionStringArray[safe:row]
//    }
    
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        self.pickedIndex  = row
        
    }
    
}
